package com.qiangesoft.design.factory.simple;

/**
 * 简单工厂模式
 */
public class SimpleFactory {
    public static void main(String[] args) {
        // 奔驰车
        Car benz = CarFactory.buildCar("benz");
        benz.running();

        // 宝马车
        Car bmw = CarFactory.buildCar("bmw");
        bmw.running();

        // 奥迪车
        Car audi = CarFactory.buildCar("audi");
        audi.running();
    }
}

/**
 * 1.工厂类
 */
class CarFactory {
    public static Car buildCar(String type) {
        if ("benz".equals(type)) {
            return new Benz();
        } else if ("bmw".equals(type)) {
            return new BMW();
        } else if ("audi".equals(type)) {
            return new Audi();
        } else {
            throw new IllegalArgumentException("Illegal argument for type: " + type);
        }
    }
}

/**
 * 2.抽象产品接口
 */
interface Car {
    void running();
}

/**
 * 3.产品实现类
 * 奔驰、宝马、奥迪
 */
class Benz implements Car {
    @Override
    public void running() {
        System.out.println("奔驰车在路上跑！");
    }
}

class BMW implements Car {
    @Override
    public void running() {
        System.out.println("宝马车在路上跑！");
    }
}

class Audi implements Car {
    @Override
    public void running() {
        System.out.println("奥迪车在路上跑！");
    }
}